package model;

import init.DBManagement;

import java.util.ArrayList;
import java.util.Map;

import model.beans.TopicBean;

public class Topics {
	public static ArrayList list(int int_unitNumber, String str_academy, String str_subject){
		ArrayList arrL_retVal = new ArrayList();
		TopicBean bn_topic;

		String str_list =
			" SELECT  TEM_TECH_KEY,                                     " +
			"       NUMERO ,  " +
			"        DESCRIPCION                 " +
			" FROM PLATEMR                            " + 
			" WHERE  UNIDAD_TK = '"+int_unitNumber +"' " ;
		
		DBManagement database = new DBManagement();
		database.executeQuery( str_list );

		for( int int_i = 0; int_i < database.getNumRows(); int_i++ ){
			bn_topic = new TopicBean();
			bn_topic.setInt_topicId(Integer.parseInt( database.getItem(int_i, 0) ));
			bn_topic.setInt_topicNumber(Integer.parseInt( database.getItem(int_i, 1) ));
			bn_topic.setStr_descripcion(database.getItem(int_i, 2));
			bn_topic.setAcademy(str_academy);
			bn_topic.setSubject(str_subject);
			bn_topic.setInt_unitNumber(int_unitNumber);
			
			arrL_retVal.add(bn_topic);
		}
		database = null;
		return arrL_retVal;
	}
	
	public static TopicBean instance( int int_topicKey, String str_academy, String str_subject ){
		TopicBean tema_retVal = null;
		String str_list = 
			" SELECT  TEM_TECH_KEY,                                     " +
			"       NUMERO ,  " +
			"        DESCRIPCION,                 " +
			"        UNIDAD_TK                          " +
			" FROM PLATEMR                            " + 
			" WHERE  TEM_TECH_KEY = '"+int_topicKey +"' " ;
		DBManagement database = new DBManagement();
	    database.executeQuery( str_list );
	  
	    if( database.getNumRows() == 1 ){
	    	tema_retVal = new TopicBean();
	    	tema_retVal.setInt_topicId(Integer.parseInt(database.getItem(0, 0)));
	    	tema_retVal.setInt_topicNumber(Integer.parseInt(database.getItem(0, 1)));
	    	tema_retVal.setStr_descripcion(database.getItem(0, 2));
	    	tema_retVal.setInt_unitNumber((Integer.parseInt(database.getItem(0, 3))));	   
	    	tema_retVal.setAcademy(str_academy);
	    	tema_retVal.setSubject(str_subject);
	    }
	    database = null;
		return tema_retVal;
	}
	
	public static int add( Map map_data, int int_unitNumber){
		int int_add;
		Integer int_topicNumber = (Integer)map_data.get("int_topicNumber");
		String str_descripcion = (String)map_data.get("str_descripcion");
		String str_add =
            " INSERT INTO PLATEMR ( "+
            "        NUMERO, "+
            "        DESCRIPCION, "+
            "        UNIDAD_TK "+
            " ) VALUES ( "+
            "        '"+int_topicNumber+"', "+
            "        '"+str_descripcion+"', "+
            "        '"+int_unitNumber+"' "+
            " ) ";
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_add );
        int_add = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_add;
	}
	
	public static int edit(Map map_data){
		int int_edit;
		Integer int_topicNumber = (Integer)map_data.get("int_topicNumber");
		String str_descripcion = (String)map_data.get("str_descripcion");
		Integer int_topicKey = (Integer)map_data.get("int_topicKey");
		String str_edit =
			 " UPDATE PLATEMR "+
	            " SET    NUMERO = '"+int_topicNumber+"', "+
	            "        DESCRIPCION =  '"+str_descripcion+"' "+	         
	            " where  TEM_TECH_KEY = "+int_topicKey;
		
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_edit );
        int_edit = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_edit;
	}
	
	public static int delete( int int_topicKey ){
		int int_delete;
		
		String str_delete =
            " delete from PLATEMR "+
            " where  TEM_TECH_KEY = "+int_topicKey;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_delete );
        int_delete = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_delete;
	}
}
